package com.logos.ruralrevitalization.controller;

import com.logos.ruralrevitalization.req.ChatReq;
import com.logos.ruralrevitalization.result.Result;
import com.logos.ruralrevitalization.service.ChatService;
import com.logos.ruralrevitalization.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Flux;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/common")
public class CommonController {

    @Resource
    private CaptchaUtil captchaUtil;

    @Resource
    private ChatService chatService;

    @Resource(name = "touristChatClient")
    private ChatClient touristChatClient;


    /**
     * 获取验证码
     */
    @GetMapping("/captcha")
    public Result<String> getCaptcha() {
        // 生成五位验证码返回
        String code = captchaUtil.generateCaptcha();
        log.info("CommonController.getCaptcha:code:{}", code);
        return Result.ok(code);
    }

//    /**
//     * ai聊天
//     */
//    @PostMapping("/chat")
//    public Result<String> chat(@RequestBody ChatReq chatReq) {
//        try {
//            if (log.isInfoEnabled()) {
//                log.info("CommonController.chat:chatReq:{}", chatReq);
//            }
//            String result = chatService.chat(chatReq);
//            return Result.ok(result);
//        } catch (Exception e) {
//            log.error("CommonController.chat:error:{}", e.getMessage(), e);
//            return Result.fail("服务器繁忙，请稍后重试");
//        }
//    }

    /**
     * ai聊天 流式输出
     */
    @PostMapping(value = "/chat",produces = "text/html;charset=utf-8")
    public Flux<String> chat(@RequestBody ChatReq chatReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("CommonController.chat:chatReq:{}", chatReq);
            }
//            Flux<String> result = chatClient.prompt()
//                    .user(chatReq.getMessage())
//                    .stream()
//                    .content();
            return touristChatClient.prompt()
                    .user(chatReq.getMessage())
                    .stream()
                    .content();
        } catch (Exception e) {
            log.error("CommonController.chat:error:{}", e.getMessage(), e);
           return null;
        }
    }

//    /**
//     * ai聊天 阻塞输出
//     */
//    @PostMapping("/chat")
//    public Result<String> chat(@RequestBody ChatReq chatReq) {
//        try {
//            if (log.isInfoEnabled()) {
//                log.info("CommonController.chat:chatReq:{}", chatReq);
//            }
//             String result = chatClient.prompt()
//                    .user(chatReq.getMessage())
//                    .call()
//                    .content();
//            return Result.ok(result);
//        } catch (Exception e) {
//            log.error("CommonController.chat:error:{}", e.getMessage(), e);
//            return Result.fail("服务器繁忙，请稍后重试");
//        }
//    }


}
